import loadJson from "../../utils/loadJson";
import Environment from "@/plugins/utils/environment";

export default Wind;
function Wind() {}
Wind._STATUS = 0;
Wind.prototype.close = function() {
    Wind._STATUS = 0;
    this.removeEvent();
    this.windy.stop();
    $("#wind").remove();
};

Wind.prototype.redraw = function() {
    let that = this;
    that.windy.stop();

    var width = that._viewer.canvas.width;
    var height = that._viewer.canvas.height;
    $("#wind")[0].width = width;
    $("#wind")[0].height = height;

    setTimeout(function() {
        that.started = that.windy.start(
            [
                [0, 0],
                [width, height]
            ],
            width,
            height
        );
        $("#wind").show();
    }, 100);
};
Wind.prototype.removeEvent = function() {
    this._viewer.camera.moveStart.removeEventListener(this._el1);
    this._viewer.camera.moveEnd.removeEventListener(this._el2);
};
Wind.prototype.addEvent = function() {
    let that = this;
    this._el1 = this._viewer.camera.moveStart.addEventListener(function() {
        if (Wind._STATUS == 0) return;
        $("#wind").hide();
        if (!!that.windy && that.started) {
            that.windy.stop();
        }
    });
    this._el2 = this._viewer.camera.moveEnd.addEventListener(function() {
        if (Wind._STATUS == 0) return;
        $("#wind").hide();
        if (!!that.windy && that.started) {
            that.redraw();
        }
    });
};
Wind.prototype.test = function() {
    var cv = $('<canvas id="wind" style="position:absolute;top:0;left:0;pointer-events:none;"></canvas>');
    this.height = window.cesiumViewer.canvas.height;
    this.width = window.cesiumViewer.canvas.width;
    cv.css("height", window.cesiumViewer.canvas.height);
    cv.css("width", window.cesiumViewer.canvas.width);
    cv.appendTo(window.cesiumViewer.canvas.parentElement);

    let that = this;
    this._viewer = window.cesiumViewer;
    window.cesiumGlobe = globe(window.cesiumViewer);

    loadJson(
        Environment.getDataBasePath() + "file/gfs.json",
        function(response) {
            that.windy = new Windy({ canvas: document.getElementById("wind"), data: response });
            that.redraw();
            that.addEvent();
            Wind._STATUS = 1;
        },
        function(errorMsg) {
            alert("请求数据失败!");
        }
    );
};
